<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 卡片头部 -->
      <div class="box-card-input">
        <el-input size="small" placeholder="菜单名称" v-model="menuName">
        </el-input>
        <el-select
          v-model="menuStatus"
          style="margin-right: 10px"
          placeholder="菜单状态"
          clearable
        >
          <!-- <el-option label="全部" value="all"></el-option> -->
          <el-option label="有效" value="1"></el-option>
          <el-option label="无效" value="0"></el-option>
        </el-select>

        <el-select
          v-model="menuType"
          style="margin-right: 10px"
          placeholder="菜单类型"
          clearable
        >
          <el-option label="目录" value="0"></el-option>
          <el-option label="菜单" value="1"></el-option>
          <el-option label="功能" value="2"></el-option>
        </el-select>

        <div class="box-card-button">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-search"
            @click="queryUserBySearch"
            >搜索</el-button
          >
          <el-button
            type="primary"
            size="small"
            icon="el-icon-plus"
            @click="add = true"
            >新增</el-button
          >
          <el-button
            type="danger"
            @click="openBatchDel"
            size="small"
            icon="el-icon-delete"
            >批量删除</el-button
          >
        </div>
      </div>

      <!-- 卡片表格区 -->
      <div class="box-card-table">
        <el-table
          :data="tableData"
          @selection-change="tableDataChange"
          ref="multipleTable"
          border
          style="width: 100%"
        >
          <el-table-column width="50" type="selection" align="center">
          </el-table-column>
          <el-table-column prop="id" width="100" label="序号" align="center">
          </el-table-column>
          <el-table-column prop="menuName" label="菜单名称" align="center">
          </el-table-column>
          <el-table-column
            prop="superMenu"
            :formatter="transformSuper"
            label="上级菜单"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="menuType"
            label="菜单类型"
            :formatter="transformType"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="menuIdentfication"
            label="菜单标识"
            width="150"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="menuAddstress"
            label="菜单地址"
            width="180"
            align="center"
          >
            <!-- </el-table-column>
          <el-table-column prop="menuId" label="排序号" align="center"> -->
          </el-table-column>
          <el-table-column prop="menuStatus" label="状态" align="center">
            <!-- eslint-disable -->
            <template slot-scope="scope">
              <el-tooltip
                :content="'当前状态: ' + transformStatus(scope.row)"
                placement="top"
              >
                <el-switch
                  v-model="scope.row.menuStatus"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  active-value="1"
                  inactive-value="0"
                  @change="statusChange(scope.row)"
                >
                </el-switch>
              </el-tooltip>
            </template>
          </el-table-column>

          <el-table-column
            fixed="right"
            label="操作"
            width="180"
            class="operation"
          >
            <!-- eslint-disable -->
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="修改"
                placement="left-start"
              >
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  circle
                  @click="openUpdate(scope.row)"
                ></el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="right-start"
              >
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="del(scope.row)"
                ></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页 -->
      <div class="page">
        <el-pagination
          @size-change="handleSizeChange"
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size="pageSize"
          @current-change="pageChange"
          @prev-click="prev"
          @next-click="next"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </el-card>

    <!-- 弹出层 -->

    <div class="add">
      <el-dialog title="新增信息" :visible.sync="add" width="40%">
        <div class="add-form">
          <el-form
            ref="addForm"
            :rules="menuRoles"
            :model="addForm"
            label-width="80px"
          >
            <el-form-item label="菜单名称" prop="menuName">
              <el-input v-model="addForm.menuName"></el-input>
            </el-form-item>

            <el-form-item label="菜单类型" clearable prop="menuType">
              <el-select v-model="addForm.menuType" placeholder="类型">
                <el-option label="目录" value="0"></el-option>
                <el-option label="菜单" value="1"></el-option>
                <el-option label="功能" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="上级菜单" clearable prop="superMenu">
              <el-select
                v-model="addForm.superMenu"
                filterable
                placeholder="请选择"
                :disabled="addForm.menuType === '0'"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.menuName"
                  :value="String(item.id)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="菜单标识" prop="menuIdentfication">
              <el-input v-model="addForm.menuIdentfication"></el-input>
            </el-form-item>
            <el-form-item label="菜单地址" prop="menuAddstress">
              <el-input v-model="addForm.menuAddstress"></el-input>
            </el-form-item>
            <el-form-item label="排序号" prop="menuID">
              <el-input v-model="addForm.menuId"></el-input>
            </el-form-item>
            <el-form-item label="菜单状态" prop="menuStatus">
              <el-select v-model="addForm.menuStatus" placeholder="状态">
                <el-option label="有效" value="1"></el-option>
                <el-option label="无效" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="add = false">取 消</el-button>
          <el-button type="primary" @click="addinfo">提 交</el-button>
        </span>
      </el-dialog>
    </div>

    <!-- 修改弹出层 -->
    <div>
      <el-dialog title="修改信息" :visible.sync="update" width="40%">
        <div class="update-form">
          <el-form
            ref="updateForm"
            :rules="menuRoles"
            :model="updateForm"
            label-width="80px"
          >
            <el-form-item label="菜单名称" prop="menuName">
              <el-input v-model="updateForm.menuName"></el-input>
            </el-form-item>

            <el-form-item label="菜单类型" clearable prop="menuType">
              <el-select v-model="updateForm.menuType" placeholder="类型">
                <el-option label="目录" value="0"></el-option>
                <el-option label="菜单" value="1"></el-option>
                <el-option label="功能" value="2"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="上级菜单" prop="superMenu">
              <el-select
                v-model="updateForm.superMenu"
                clearable
                placeholder="请选择"
                :disabled="updateForm.menuType === '2' ? true : false"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.menuName"
                  :value="String(item.id)"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="菜单标识" prop="menuIdentfication">
              <el-input v-model="updateForm.menuIdentfication"></el-input>
            </el-form-item>
            <el-form-item label="菜单地址" prop="menuAddstress">
              <el-input v-model="updateForm.menuAddstress"></el-input>
            </el-form-item>
            <el-form-item label="排序号" prop="menuId">
              <el-input v-model="updateForm.menuId"></el-input>
            </el-form-item>
            <el-form-item label="菜单状态" prop="menuStatus">
              <el-select v-model="updateForm.menuStatus" placeholder="状态">
                <el-option label="有效" value="1"></el-option>
                <el-option label="无效" value="0"></el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="update = false">取 消</el-button>
          <el-button type="primary" @click="updateinfo">提 交</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import has from "../../directives/has.js";
export default {
  directives: {
    has,
  },
  inject: ["reload"],
  data() {
    return {
      loading: false,
      options: [],
      menuList: [],
      multipleSelection: [],
      roler: false,
      update: false,
      imports: false,
      add: false,
      date: "",
      value: true,
      checked: true,
      menuName: "",
      menuType: "",
      menuStatus: "",
      currentPage: 1,
      total: 1,
      pageSize: 4,
      pageSizes: [4, 5, 6, 7, 8],
      tableData: [],
      addForm: {
        menuName: "",
        superMenu: null,
        menuType: "",
        menuIdentfication: "",
        menuAddstress: "",
        menuId: "",
        menuStatus: "1",
      },
      updateForm: {
        menuName: "",
        superMenu: null,
        menuType: "",
        menuIdentfication: "",
        menuAddstress: "",
        menuId: "",
        menuStatus: "",
      },
      menuRoles: {
        menuName: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
        ],
        // superMenu:[{ required: true, message: '请输入上级菜单', trigger: 'blur' },],
        menuType: [
          { required: true, message: "请输入菜单类型", trigger: "blur" },
        ],
        menuIdentfication: [
          { required: true, message: "请输入菜单标识", trigger: "blur" },
        ],
        // menuAddstress:[{ required: true, message: '请输入活动名称', trigger: 'blur' },],
        menuId: [{ required: true, message: "请输入排序号", trigger: "blur" }],
        menuStatus: [
          { required: true, message: "请输入菜单状态", trigger: "blur" },
        ],
      },
      idList: [],
      aaa: [],
    };
  },
  created() {
    this.getMenuList();
    this.queryUserBySearch();
    this.getData();
  },
  methods: {
    transformSuper: function (row) {
      let menu = row.superMenu;
      console.log(Number(menu));
      console.log(this.aaa);
      this.aaa.forEach((item) => {
        if (Number(menu) === item.id) {
          menu = item.menuName;
        }
      });
      return menu;
      // console.log(menu);
    },
    transformType: function (row) {
      let type = row.menuType;
      switch (type) {
        case "0":
          type = "目录";
          break;
        case "1":
          type = "菜单";
          break;
        case "2":
          type = "功能";
          break;
      }
      return type;
    },
    //显示状态值
    transformStatus: function (row) {
      if (row.menuStatus === "1") {
        return "有效";
      }
      return "无效";
    },
    //新增菜单时，自动获取父级菜单

    getMenuList: function () {
      this.$axios
        .post("/api/menu/queryMenuByType")
        .then((res) => {
          res.data.data.forEach((item) => {
            if (item.menuType != "2") {
              this.options.push(item);
            }
          });
          console.log(this.options);
        })
        .catch((err) => {
          console.log(err);
        });
    },

    //批量删除对话框
    openBatchDel() {
      this.$confirm("此操作将永久删除信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post("/api/menu/menuBatchDelete", this.idList)
            .then((response) => {
              console.log(response);
              if (response.data.code === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.reload();
              } else {
                this.$message({
                  showClose: false,
                  type: "warning",
                  message: response.data.message,
                  duration: 1000,
                });
              }
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //批量删除
    tableDataChange(val) {
      let temp = val;
      console.log(val);
      temp.forEach((element) => {
        this.idList.push(element.id);
      });
      console.log(this.idList);
    },
    //表格多选相关函数
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //搜索相关函数
    queryUserBySearch: function () {
      this.$axios
        .get("api/menu/queryMenusBySearch", {
          params: {
            menuName: this.menuName,
            menuStatus: this.menuStatus,
            menuType: this.menuType,
            currentPage: this.currentPage,
            pageSize: this.pageSize,
          },
        })
        .then((response) => {
          console.log(response);
          this.tableData = response.data.data;
          this.total = response.data.total;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    //每页数据展示条数改变函数
    handleSizeChange: function (val) {
      this.queryUserBySearch();
    },
    //当前页改变函数

    pageChange(currentPage) {
      this.currentPage = currentPage;
      this.queryUserBySearch();
    },
    prev() {
      this.currentPage--;
      this.queryUserBySearch();
    },
    next() {
      this.currentPage++;
      this.queryUserBySearch();
    },

    //改变用户状态函数

    //获取数据函数
    getData: function () {
      this.$axios
        .get("/api/menu/queryMenusList", {
          params: {
            count: this.pageSize,
            currentPage: this.currentPage,
          },
        })
        .then((response) => {
          console.log(response);
          this.aaa = response.data.data;
        })
        .catch((error) => {
          console.log(error);
        });
    },

    //增加数据函数
    addinfo: function () {
      console.log(this.addForm);
      this.$axios
        .post("/api/menu/insertMenus", this.addForm)
        .then((response) => {
          console.log(response);
          if (response.data.code === 200) {
            this.$message({
              showClose: false,
              type: "success",
              message: "添加信息成功!",
              duration: 1000,
            });
            this.add = false;
            this.reload();
          } else {
            this.$message({
              showClose: false,
              type: "warning",
              message: response.data.message,
              duration: 1000,
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },

    //更新数据函数
    openUpdate: function (row) {
      // console.log(row);
      this.update = true;
      this.updateForm = row;
      // console.log(this.updateForm);
    },
    updateinfo: function () {
      this.$axios
        .post("/api/menu/updateMenus", this.updateForm)
        .then((response) => {
          console.log(response);
          if (response.data.code === 200) {
            this.$message({
              showClose: false,
              type: "success",
              message: "修改信息成功!",
              duration: 1000,
            });
            this.update = false;
            this.reload();
          } else {
            this.$message({
              showClose: false,
              type: "warning",
              message: response.data.message,
              duration: 1000,
            });
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },

    //删除数据函数
    del: function (row) {
      this.$confirm("此操作将永久删除信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$axios
            .post("/api/menu/deleteMenus", row.id)
            .then((response) => {
              console.log(response);
              if (response.data.code === 200) {
                this.$message({
                  showClose: false,
                  type: "success",
                  message: "删除信息成功!",
                  duration: 1000,
                });
                this.reload();
              } else {
                this.$message({
                  showClose: false,
                  type: "warning",
                  message: response.data.message,
                  duration: 1000,
                });
              }
            })
            .catch((error) => {
              console.log(error);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //修改状态
    statusChange: function (row) {
      (this.updateForm = {
        id: row.id,
        menuName: row.menuName,
        superMenu: row.superMenu,
        menuType: row.menuType,
        menuIdentfication: row.menuIdentfication,
        menuAddstress: row.menuAddstress,
        menuId: row.menuId,
        menuStatus: row.menuStatus,
      }),
        this.$axios
          .post("/api/menu/updateStatus", this.updateForm)
          .then((response) => {
            if (response.data.status === 200) {
              this.$message({
                type: "success",
                message: "修改成功",
              });
              this.reload();
            } else {
              this.$message({
                showClose: false,
                type: "warning",
                message: response.data.message,
                duration: 1000,
              });
            }
          })
          .catch((error) => {
            console.log(error);
          });
    },
  },
};
</script>

<style scoped>
.box-card .box-card-input {
  margin-bottom: 15px;
}
.box-card .box-card-input .el-input {
  display: inline-block;
  width: 200px;
  margin-right: 15px;
  margin-bottom: 10px;
}

.el-form .el-input {
  width: 250px;
}

.el-form .el-select {
  width: 250px;
}
</style>